<?php
namespace app\index\controller;

use think\Controller;
use app\index\model\Customer;
use think\Db;
// use think\Request;

class Corder extends Controller
{
    public function corder(){
        if(empty(session('email'))){
            $this->error('请先登录!','login/login');
        }
        
        //显示收货人
        $data = Customer::where('email',session('email'))->select();
        //将默认收货人放在最前面
        for ($i = 0; $i < count($data); $i++) {
            // 默认地址
            if (strcmp($data[$i]->cdefault, '1') == 0) {
                $c = $data[$i];
                unset($data[$i]);
                array_unshift($data, $c);
                break;
            }
        }
        $this->assign('Customers',$data);
        
        //勾选的购物车商品显示
        $cartIDs = input('get.cartIDs');
        // dump(explode(',', $cartIDs));
        $cartIDs = explode(',', $cartIDs);

        $result = Db::table('vcart')->where('cartID', 'in', $cartIDs)->where('email', session('email'))->select();
        $this->assign('carts',$result);
        session("cartIDs", $cartIDs);
        
        return $this->fetch();
    }
}